<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增友情链接')" />
    <th:block th:include="include :: datetimepicker-css" />
    <style>
        .success{
            color: greenyellow;
        }
        .error{
            color: orangered;
        }
    </style>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-link-add">
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">链接名称：</label>
            <div class="col-sm-8">
                <input name="linkName" class="form-control" type="text" required>
                <span class="linkName"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">链接网址：</label>
            <div class="col-sm-8">
                <input name="linkUrl" class="form-control" type="text" required>
                <span class="linkUrl"/>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">排序：</label>
            <div class="col-sm-8">
                <input name="sortOrder" class="form-control" type="text" required>
                <span class="sortOrder">数字越大排的越前面,范围0-999</span>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">是否显示：</label>
            <div class="col-sm-8">
                <select name="isShow" class="form-control">
                    <option value="1">是</option>
                    <option value="0">否</option>
                </select>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: datetimepicker-js" />
<script th:inline="javascript">
    var prefix = ctx + "friendship/link"
    $("#form-link-add").validate({
        focusCleanup: true
    });

    //内容校验
    var linkName = linkUrl = order = 0;
    $("input[name=linkName]").on('change',function () {
        var text = $("input[name=linkName]").val();
        var names = [[${linkNames}]];
        if ($.common.isEmpty(text)){
            $(".linkName").addClass('error').removeClass('success').html("请填写链接名称");linkName = 0;
        }else if (names.includes(text)){
            $(".linkName").addClass('error').removeClass('success').html("该值不可用，系统中已存在！");linkName = 0;
        }else {
            $(".linkName").addClass('success').removeClass('error').html("验证通过");linkName = 1;
        }
    })
    $("input[name=linkUrl]").on('change',function () {
        var text = $("input[name=linkUrl]").val();
        var codes = [[${linkUrls}]];
        if ($.common.isEmpty(text)){
            $(".linkUrl").addClass('error').removeClass('success').html("请填写链接网址");linkUrl = 0;
        }else if (codes.includes(text)){
            $(".linkUrl").addClass('error').removeClass('success').html("该值不可用，系统中已存在！");linkUrl = 0;
        }else {
            $(".linkUrl").addClass('success').removeClass('error').html("验证通过");linkUrl = 1;
        }
    })
    $("input[name=sortOrder]").on("change",function () {
        var text = $("input[name=sortOrder]").val();
        var reg = /^[0-9][0-9]{0,2}$/;
        var flag = reg.test(text);
        if (flag){
            $(".sortOrder").removeClass('error').addClass('success').html("验证通过");order = 1;
        }else {
            $('.sortOrder').addClass('error').removeClass('success').html("请填写排序,范围0-999");order = 0;
        }
    })

    function submitHandler() {
        if (linkName != 1 || linkUrl != 1 || order != 1){return;}
        if ($.validate.form()) {
            $.operate.save(prefix + "/add", $('#form-link-add').serialize());
        }
    }

    $("input[name='createDate']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });

    $("input[name='updateDate']").datetimepicker({
        format: "yyyy-mm-dd",
        minView: "month",
        autoclose: true
    });
</script>
</body>
</html>
